package totoCharts;
import javax.servlet.http.*;
import java.util.List;
import javax.jdo.PersistenceManager;

/*
 * this class returns an old form to the gadget, based on 2 parameters that the
 * gadget sends (waveID and date).
 */
public class gadgetRequestOldForm extends HttpServlet 
{    
	/**
	 * 
	 */
	private static final long serialVersionUID = 1103249922466496297L;

	@SuppressWarnings("unchecked")
	public void doGet(HttpServletRequest req, HttpServletResponse resp)
	 
	{
		PersistenceManager pm=null;
		try
		{
			String result="error";
			
			String param= req.getQueryString();
			String[] params=param.split("~");
			
			//getting op1(waveID) as an argument from the url
			int op1=Integer.parseInt(params[0]);
		
			//getting op2(Date) as an argument from the url
			String temp=params[1];
			if (temp.contains("&")) temp=temp.substring(0,temp.indexOf('&'));
			
			//op2 will now be in the format that we need for the query
			String op2="'"+temp+"'";
		
			
			pm = guestbook.PMF.get().getPersistenceManager();
			javax.jdo.Query query = pm.newQuery(oldForm.class);
			query.setFilter("waveID =="+ op1+ "&& date =="+op2);
			List<oldForm> list = (List<oldForm>) pm.newQuery(query).execute();
		    
		    for (oldForm object : list) 
		    {
		    	result=object.getForm();
		    }
		    
		    resp.setContentType("text/plain;charset=UTF-8");            
			resp.getWriter().println(result);
			}
		catch (Exception e) // catches exception for the gadget
		{ 
			e.printStackTrace();
		}
		finally
		{
			if (pm!=null) pm.close();
		}
			    
	}
}



